package com.teradata

import org.eclipse.jetty.server.Server
import org.eclipse.jetty.servlet.DefaultServlet
import org.eclipse.jetty.webapp.WebAppContext
import org.scalatra.servlet.ScalatraListener

object JettyLauncher {
  val DEFAULT_SERVICE_PORT: Int = 8081

  //  val LOGGER = LoggerFactory.getLogger(JettyLauncher.getClass)

  def main(args: Array[String]): Unit = {
    val servicePort = System.getenv("IOP_REAL_PARSE_PORT")
    val port = if (servicePort != null) servicePort.toInt else DEFAULT_SERVICE_PORT

    val server = new Server(port)
    val context = new WebAppContext()
    context.setContextPath("/")
    val resourceBase = JettyLauncher.getClass
      .getClassLoader
      .getResource("webapp")
      .toExternalForm

    println(resourceBase)


    context.setResourceBase(resourceBase)
    context.addEventListener(new ScalatraListener)
    context.setInitParameter(ScalatraListener.LifeCycleKey, "com.teradata.ScalatraBootstrap")
    context.addServlet(classOf[DefaultServlet], "/*")

    server.setHandler(context)
    //    LOGGER.info(s"Starting Jetty server on port : $port")

    server.start()
    server.join()
  }
}
